﻿Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim table As DataTable
        Dim tabl2 As DataTable
        table = MakeNamesTable()
        tabl2 = MakeNamesTabl2()

        ' Once a table has been created, use the 
        ' NewRow to create a DataRow.
        Dim row1 As DataRow
        row1 = table.NewRow()
        row1("fName") = "A1"
        row1("lName") = "B1"
        row1("V1") = 180
        table.Rows.Add(row1)

        Dim row2 As DataRow
        row2 = table.NewRow()
        row2("fName") = "A2"
        row2("lName") = "B2"
        row2("V2") = 180
        table.Rows.Add(row2)

        Dim row3 As DataRow
        row3 = table.NewRow()
        row3("fName") = "A3"
        row3("lName") = "B3"
        row3("V1") = 180
        row3("V2") = 360
        table.Rows.Add(row3)

        'add item to tabl2

        Dim rov2 As DataRow
        rov2 = tabl2.NewRow()
        rov2("fName") = "A2"
        rov2("lName") = "B2"
        rov2("V2") = 180
        tabl2.Rows.Add(rov2)

        Dim rov3 As DataRow
        rov3 = tabl2.NewRow()
        rov3("fName") = "A3"
        rov3("lName") = "B3"
        rov3("V1") = 180
        rov3("V2") = 360
        tabl2.Rows.Add(rov3)



        Dim column As DataColumn
        For Each column In table.Columns
            Console.WriteLine(column.ColumnName)
        Next

        Dim rowidx As DataRow
        For Each rowidx In table.Rows
            'Console.WriteLine(rowidx.Item(0))
            rowidx.Item(5) = 0
            If (Not rowidx.IsNull(4)) And (Not rowidx.IsNull(3)) Then
                'rowidx.Item(5) = 1
                rowidx.Item(5) = Math.Round(rowidx.Item(4) / rowidx.Item(3))

            End If
        Next

        Me.DataGridView1.DataSource = table

        Dim V4 As DataGridViewComboBoxColumn = New DataGridViewComboBoxColumn()
        V4.HeaderText = "V4"
        V4.DataSource = tabl2
        V4.DisplayMember = "fName"
        Me.DataGridView1.Columns.Add(V4)

        For i = 0 To DataGridView1.Rows.Count

            Console.WriteLine(DataGridView1.Rows.Item(i).Cells.Item(0).Value)
        Next

        'for (int i = 0; i < DataGridView1.Rows.Count; i++)
        '{
        'dvg.Rows[i].Cells["ComboColumnSample"].Value = (cmb.Items[0] as 
        'DataRowView).Row[1].ToString();
        '}
    End Sub

    Private Function MakeNamesTabl2() As DataTable
        ' Create a new DataTable titled 'Names.'
        Dim namesTable As DataTable = New DataTable("Name2")

        ' Add three column objects to the table.
        Dim idColumn As DataColumn = New DataColumn()
        idColumn.DataType = System.Type.GetType("System.Int32")
        idColumn.ColumnName = "id"
        idColumn.AutoIncrement = True
        namesTable.Columns.Add(idColumn)

        Dim fNameColumn As DataColumn = New DataColumn()
        fNameColumn.DataType = System.Type.GetType("System.String")
        fNameColumn.ColumnName = "Fname"
        fNameColumn.DefaultValue = "Fname"
        namesTable.Columns.Add(fNameColumn)

        Dim lNameColumn As DataColumn = New DataColumn()
        lNameColumn.DataType = System.Type.GetType("System.String")
        lNameColumn.ColumnName = "LName"
        namesTable.Columns.Add(lNameColumn)

        Dim V1 As DataColumn = New DataColumn()
        V1.DataType = System.Type.GetType("System.Int32")
        V1.ColumnName = "V1"
        namesTable.Columns.Add(V1)

        Dim V2 As DataColumn = New DataColumn()
        V2.DataType = System.Type.GetType("System.Int32")
        V2.ColumnName = "V2"
        namesTable.Columns.Add(V2)

        Dim V3 As DataColumn = New DataColumn()
        V3.DataType = System.Type.GetType("System.Int32")
        V3.ColumnName = "V3"
        namesTable.Columns.Add(V3)

        ' Create an array for DataColumn objects.
        Dim keys(0) As DataColumn
        keys(0) = idColumn
        namesTable.PrimaryKey = keys

        ' Return the new DataTable.
        MakeNamesTabl2 = namesTable
    End Function

    Private Function MakeNamesTable() As DataTable
        ' Create a new DataTable titled 'Names.'
        Dim namesTable As DataTable = New DataTable("Names")

        ' Add three column objects to the table.
        Dim idColumn As DataColumn = New DataColumn()
        idColumn.DataType = System.Type.GetType("System.Int32")
        idColumn.ColumnName = "id"
        idColumn.AutoIncrement = True
        namesTable.Columns.Add(idColumn)

        Dim fNameColumn As DataColumn = New DataColumn()
        fNameColumn.DataType = System.Type.GetType("System.String")
        fNameColumn.ColumnName = "Fname"
        fNameColumn.DefaultValue = "Fname"
        namesTable.Columns.Add(fNameColumn)

        Dim lNameColumn As DataColumn = New DataColumn()
        lNameColumn.DataType = System.Type.GetType("System.String")
        lNameColumn.ColumnName = "LName"
        namesTable.Columns.Add(lNameColumn)

        Dim V1 As DataColumn = New DataColumn()
        V1.DataType = System.Type.GetType("System.Int32")
        V1.ColumnName = "V1"
        namesTable.Columns.Add(V1)

        Dim V2 As DataColumn = New DataColumn()
        V2.DataType = System.Type.GetType("System.Int32")
        V2.ColumnName = "V2"
        namesTable.Columns.Add(V2)

        Dim V3 As DataColumn = New DataColumn()
        V3.DataType = System.Type.GetType("System.Int32")
        V3.ColumnName = "V3"
        namesTable.Columns.Add(V3)

        ' Create an array for DataColumn objects.
        Dim keys(0) As DataColumn
        keys(0) = idColumn
        namesTable.PrimaryKey = keys

        ' Return the new DataTable.
        MakeNamesTable = namesTable
    End Function

End Class
